//    Openbravo POS is a point of sales application designed for touch screens.
//    Copyright (C) 2007-2009 Openbravo, S.L.
//    http://www.openbravo.com/product/pos
//
//    This file is part of Openbravo POS.
//
//    Openbravo POS is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Openbravo POS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Openbravo POS.  If not, see <http://www.gnu.org/licenses/>.
package com.openbravo.bean.sales;

import com.openbravo.bean.shard.IncludeTaxs;
import cn.ekuma.data.dao.bean.I_ModifiedLogBean;
import com.openbravo.bean.Customer;
import com.openbravo.bean.PaymentInfo;
import com.openbravo.bean.infobean.UserInfo;
import com.openbravo.def.I_CalcTaxes;
import com.openbravo.def.I_TaxCalcLine;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author adrianromero
 */
public class Ticket implements I_ModifiedLogBean<String> ,I_CalcTaxes{

    public static final long serialVersionUID = 2765650092387265178L;

    public static final int RECEIPT_NORMAL = 0;
    public static final int RECEIPT_REFUND = 1;
    public static final int RECEIPT_PAYMENT = 2;

    public static final String AUTOCREATE_NAME="autoName";

    public String m_sId;
    public int ticketType;
    public int m_iTicketId;
    public java.util.Date m_dDate;
    public HashMap<String,String> attributes;
    public UserInfo m_User;
    public Customer m_Customer;
    public String m_sActiveCash;
    public List<TicketLine> m_aLines;
    public List<PaymentInfo> payments;
    public List<IncludeTaxs> taxes;
    public String m_sResponse;

    public double m_dPriceRate;//整单折扣率
    public String m_sGroupName;//单据分组
    private Date lastModified;
    
    public double totalPrice;
    public double totalTax;
    public double totalConsultCost;
    private String location;
    
    private String hostName;
    
    public double totalScore;
    
    private String saleManager;

    /** Creates new TicketModel */
    public Ticket() {
        ticketType = RECEIPT_NORMAL;
        m_iTicketId = 0; // incrementamos
        m_dDate = new Date();
        attributes = new HashMap();
        m_User = null;
        m_Customer = null;
        m_sActiveCash = null;
        m_aLines = new ArrayList<TicketLine>(); // vacio de lineas

        m_dPriceRate=1;

        payments = new ArrayList<PaymentInfo>();
        taxes = null;
        m_sResponse = null;
        m_sGroupName=null;
        totalScore=0;
    }

    public Ticket copyTicket() {
        Ticket t = new Ticket();

        t.ticketType = ticketType;
        t.m_iTicketId = m_iTicketId;
        t.m_dDate = m_dDate;
        t.m_sActiveCash = m_sActiveCash;
        t.attributes = (HashMap) attributes.clone();
        t.m_User = m_User;
        t.m_Customer = m_Customer;
        t.m_dPriceRate=m_dPriceRate;
        t.m_sGroupName=m_sGroupName;

        t.m_aLines = new ArrayList<TicketLine>();
        for (TicketLine l : m_aLines) {
            t.m_aLines.add(l.copyTicketLine());
        }
        t.refreshLines();

        t.payments = new ArrayList<PaymentInfo>();
        for (PaymentInfo p : payments) {
            t.payments.add(p.copyPayment());
        }

        t.totalScore=totalScore;
        t.saleManager=saleManager;
        // taxes are not copied, must be calculated again.

        return t;
    }

    public String getId() {
        return m_sId;
    }

    public int getTicketType() {
        return ticketType;
    }

    public void setTicketType(int tickettype) {
        this.ticketType = tickettype;
    }

    public int getTicketId() {
        return m_iTicketId;
    }

    public void setTicketId(int iTicketId) {
        m_iTicketId = iTicketId;
    // refreshLines();
    }

    public java.util.Date getDate() {
        return m_dDate;
    }

    public void setDate(java.util.Date dDate) {
        m_dDate = dDate;
    }

    public UserInfo getUser() {
        return m_User;
    }

    public void setUser(UserInfo value) {
        m_User = value;
    }

    public Customer getCustomer() {
        return m_Customer;
    }

    public void setCustomer(Customer value) {
        m_Customer = value;
    }

    public String getCustomerId() {
        if (m_Customer == null) {
            return null;
        } else {
            return m_Customer.getId();
        }
    }

    public void setActiveCash(String value) {
        m_sActiveCash = value;
    }

    public String getActiveCash() {
        return m_sActiveCash;
    }

    public String getProperty(String key) {
        return attributes.get(key);
    }

    public String getProperty(String key, String defaultvalue) {
        String ret=attributes.get(key);
        return ret!=null?ret:defaultvalue;
    }

    public void setProperty(String key, String value) {
        attributes.put(key, value);
    }

    public HashMap getProperties() {
        return attributes;
    }

    public TicketLine getLine(int index) {
        return m_aLines.get(index);
    }

    public void addLine(TicketLine oLine) {

        oLine.setTicket(m_sId, m_aLines.size());
        m_aLines.add(oLine);
    }

    public void insertLine(int index, TicketLine oLine) {
        m_aLines.add(index, oLine);
        refreshLines();
    }

    public void setLine(int index, TicketLine oLine) {
        oLine.setTicket(m_sId, index);
        m_aLines.set(index, oLine);
    }

    public void removeLine(int index) {
        m_aLines.remove(index);
        refreshLines();
    }

    public void refreshLines() {
        for (int i = 0; i < m_aLines.size(); i++) {
            getLine(i).setTicket(m_sId, i);
        }
    }

    public int getLinesCount() {
        return m_aLines.size();
    }

    public String getM_sGroupName() {
        return m_sGroupName;
    }

    public void setM_sGroupName(String m_sGroupName) {
        this.m_sGroupName = m_sGroupName;
    }

    public List<TicketLine> getLines() {
        return m_aLines;
    }

    public void setLines(List<TicketLine> l) {
        m_aLines = l;
    }

    public List<PaymentInfo> getPayments() {
        return payments;
    }

    public void setPayments(List<PaymentInfo> l) {
        payments = l;
    }

    public void resetPayments() {
        payments = new ArrayList<PaymentInfo>();
    }

    public List<IncludeTaxs> getTaxes() {
        return taxes;
    }

    public boolean hasTaxesCalculated() {
        return taxes != null;
    }

    public void setTaxes(List<IncludeTaxs> l) {
        taxes = l;
    }

    public void resetTaxes() {
        taxes = null;
    }

   public  boolean isGroupTicket() {
        return m_sGroupName != null && !m_sGroupName.isEmpty();
    }

    public double getM_dPriceRate() {
        return m_dPriceRate;
    }

    public void setM_dPriceRate(double rate) {
        m_dPriceRate = rate;
    }

    public boolean isGroupPayTicket() {
        String autoCreateName=getProperty(AUTOCREATE_NAME,null);
        if(autoCreateName==null||autoCreateName.isEmpty())
            return false;
        return true;
    }

    public HashMap<String, String> getAttributes() {
        return attributes;
    }

    public void setAttributes(HashMap<String, String> attributes) {
        this.attributes = attributes;
    }

    public Customer getM_Customer() {
        return m_Customer;
    }

    public void setM_Customer(Customer m_Customer) {
        this.m_Customer = m_Customer;
    }

    public UserInfo getM_User() {
        return m_User;
    }

    public void setM_User(UserInfo m_User) {
        this.m_User = m_User;
    }

    public List<TicketLine> getM_aLines() {
        return m_aLines;
    }

    public void setM_aLines(List<TicketLine> m_aLines) {
        this.m_aLines = m_aLines;
    }

    public Date getM_dDate() {
        return m_dDate;
    }

    public void setM_dDate(Date m_dDate) {
        this.m_dDate = m_dDate;
    }

    public int getM_iTicketId() {
        return m_iTicketId;
    }

    public void setM_iTicketId(int m_iTicketId) {
        this.m_iTicketId = m_iTicketId;
    }

    public String getM_sActiveCash() {
        return m_sActiveCash;
    }

    public void setM_sActiveCash(String m_sActiveCash) {
        this.m_sActiveCash = m_sActiveCash;
    }

    public String getM_sId() {
        return m_sId;
    }

    public void setM_sId(String m_sId) {
        this.m_sId = m_sId;
    }

    public String getM_sResponse() {
        return m_sResponse;
    }

    public void setM_sResponse(String m_sResponse) {
        this.m_sResponse = m_sResponse;
    }

    public String getKey() {
        return m_sId;
    }

    public Date getLastModified() {
        return lastModified;
    }

    public void setLastModified(Date lastModified) {
        this.lastModified = lastModified;
    }

    public void setKey(String key) {
        this.m_sId=key;
    }

    public List<I_TaxCalcLine> getTaxCalcLines() {
        List<I_TaxCalcLine> line=new ArrayList<I_TaxCalcLine>();
        for(TicketLine l:m_aLines)
            line.add(l);
       return line;
    }

    public int getTaxType() {
        return TAXTYPE_Ticket;
    }

	public double getTotalPrice() {
		return totalPrice;
	}

	public void setTotalPrice(double totalPrice) {
		this.totalPrice = totalPrice;
	}

	public double getTotalTax() {
		return totalTax;
	}

	public void setTotalTax(double totalTax) {
		this.totalTax = totalTax;
	}

	public double getTotalConsultCost() {
		return totalConsultCost;
	}

	public void setTotalConsultCost(double totalConsultCost) {
		this.totalConsultCost = totalConsultCost;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getHostName() {
		return hostName;
	}

	public void setHostName(String hostName) {
		this.hostName = hostName;
	}

	public double getTotalScore() {
		return totalScore;
	}

	public void setTotalScore(double totalScore) {
		this.totalScore = totalScore;
	}

	public String getSaleManager() {
		return saleManager;
	}

	public void setSaleManager(String saleManager) {
		this.saleManager = saleManager;
	}
	
}


